package com.example.syyao.myapp.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.syyao.myapp.CircleImageView;
import com.example.syyao.myapp.Personal.Personal_visitor;
import com.example.syyao.myapp.Response.persoal_heart_bean;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;

import java.util.List;
import java.util.Map;

public class personal_heart_adapter extends BaseAdapter{
    private Get_response conner_response;
    private Context context;
    private List<Map<String,Object>> data;
    persoal_heart_bean bean=null;
    private  String username;
    //为1是关注，2是粉丝
    private  int flag;
    public personal_heart_adapter (Context context, List<Map<String,Object>> data,String username,int flag){
        this.context=context;
        this.data=data;
        this.username = username;
        this.flag = flag;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

            bean=new persoal_heart_bean();
            LayoutInflater layoutInflater=LayoutInflater.from(context);
            view=layoutInflater.inflate(R.layout.personal_list_heart,null);
            bean.heart_head=(CircleImageView) view.findViewById(R.id.personal_heart_head);
            bean.heart_txt=(TextView)view.findViewById(R.id.personal_heart_id);
            bean.heart_btn=(Button) view.findViewById(R.id.personal_heart_cancel);
            bean.heart_lay=(LinearLayout)view.findViewById(R.id.info_people);
            view.setTag(bean);

        //数据绑定
        //bean.heart_head.setBackgroundResource((int)data.get(position).get("personal_heart_head"));//利用这个无法是图片圆形
        //列表图片
        Bitmap bm=BitmapFactory.decodeResource(context.getResources(),(int)data.get(position).get("personal_heart_head"));
        Bitmap bitmap = CircleImageView.getCroppedBitmap(bm,400);
//
        RequestOptions options = new RequestOptions().placeholder(R.mipmap.login_pic);
        Glide.with(context).asBitmap().load("http://120.79.205.71/picture/"+data.get(position).get("head_picture").toString()).apply(options).into(bean.heart_head);
//        bean.heart_head.setImageBitmap(bitmap);
        //用户昵称
        bean.heart_txt.setText((String)data.get(position).get("personal_heart_id"));
        bean.id = data.get(position).get("Account_id").toString();
        System.out.println(data.get(position).get("Account_id").toString());
        //事件绑定
        bean.heart_lay.setOnClickListener(new heat_btn(bean.id));

        bean.heart_btn.setOnClickListener(new heat_btn_cancel(bean.id));

        return view;
    }

    public class heat_btn implements View.OnClickListener{


        private  String userid;
        public heat_btn(String userid) {
            this.userid = userid;
        }

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, Personal_visitor.class);
            if(flag == 1){
                intent.putExtra("status","conner");
                intent.putExtra("id",userid);
            }else{
                intent.putExtra("status","fans");
                intent.putExtra("id",userid);
            }
            context.startActivity(intent);
        }
    }

    public class heat_btn_cancel implements View.OnClickListener{

        private  String userid;
        public heat_btn_cancel(String userid) {
            this.userid = userid;
        }

        @Override
        public void onClick(View view) {
            if(flag == 1) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
//                System.out.println(username);
                        String webContent = WebAccessTools.canel_Conner(userid, username);
                        System.out.println("web:" + webContent);
                        conner_response = (Get_response) JsonUtil.jsonToObject(webContent,
                                Get_response.class);
                        handler.sendEmptyMessage(0x123);
                    }
                }).start();
            }else{
                new Thread(new Runnable() {

                    @Override
                    public void run() {
//                System.out.println(username);
                        String webContent = WebAccessTools.canel_fans(userid, username);
                        System.out.println("web:" + webContent);
                        conner_response = (Get_response) JsonUtil.jsonToObject(webContent,
                                Get_response.class);
                        handler.sendEmptyMessage(0x123);
                    }
                }).start();
            }
        }

        @SuppressLint("HandlerLeak")
        private Handler handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                if (conner_response != null && conner_response.getMessage().equals("成功")) {
                    notifyDataSetChanged();
                }
            };
        };
    }


}
